<template>
    <rui-page :defines="defines">
        <rui-query :label-width="200"
                   ref="searchForm"
                   :query="queryData"
                   :query-rows="searchFormOptions"
                   :more-rows="moreRows"
        />
        <rui-table border
                   :defineId="'voucherInfoDifine'"
                   :fixedLeft="['action']"
                   :showIndex="false"
                   :select="false"
                   :columns="infoColumns"
                   :slots="[{key:'action',slot:'action'}]"
                   @loadDatas="queryData"
                   ref-query="searchForm"
                   ref="infoTable"
        >
            <template slot="toolBar">

                <Button @click="exportData" type="primary" icon="md-list-box">导出报表</Button>
            </template>

            <template slot="action" slot-scope="{ row }">
                <div>
                    <Button @click="resend(row)" size="small" type="warning" v-if="row.status=='sendFail'">重新发送
                    </Button>
                    <Button @click="reversal(row)" size="small" type="error" v-if="row.status=='sendSuccess'">红冲
                    </Button>
                    <Button @click="view(row)" size="small" type="primary">查看</Button>
                    <circleLoading v-if="operationLoading"/>
                </div>
            </template>
        </rui-table>
        <Modal v-model="isShowModel" :title="modelTitle" :width="1200" :closable="false"
               :mask-closable="false">
            <rui-form :form-options="infoFormOptions" :read-only="true"
                      ref="infoForm"></rui-form>
            <rui-table
                :defineId="'voucherEntryDifine'"
                :showIndex="false"
                :select="false"
                :pageAble="false"
                :columns="entryColumns"
                ref="entryTable"
            >
            </rui-table>
            <div slot="footer">
                <Button size="small" @click="isShowModel = false">返回</Button>
            </div>
        </Modal>
        <div v-show="false">
            <Table border :columns="columns"  :data="data" ref="voucherInfo">
            </Table>
        </div>
    </rui-page>

</template>
<script>
    import voucherInfoDifine from "../defines/voucher-info-define";
    import voucherEntryDifine from "../defines/voucher-entry-define";
    import circleLoading from "_c/circle-loading/circle-loading.vue";
    import {
        getAccountVoucherInfoList,
        getVoucherInfo,
        reversal,
        resend,
        exportVoucherList
    } from "@/projects/basic/api/contract/voucher/voucherInfo";

    export default {
        name: "product-atom",
        components: {
            circleLoading
        },
        data() {
            return {
                operationLoading: false,
                submitLoading: false,
                isShowModel: false,
                modelTitle: null,
                searchFormOptions: [
                    {
                        defineId: "voucherInfoDifine",
                        span: 6,
                        fields: ["contractNo", "dealerName", "explanation"]
                    }
                ],
                moreRows: [
                    {
                        defineId: "voucherInfoDifine",
                        span: 6,
                        fields: ['voucherNo', "createStartDate", "createEndDate"]
                    }, {
                        defineId: "voucherInfoDifine",
                        span: 6,
                        fields: ["status"]
                    }
                ],
                infoColumns: [
                    'action', 'status', 'voucherNo', 'mainBusiness', 'childBusiness', 'dealerName', 'contractNo', 'explanation', 'postingDate', 'sendingDate', 'reversalVoucherNo', 'errMsg', 'createBy', 'createTime', 'updateBy', 'updateTime'
                ],
                infoFormOptions: [{
                    grids: [
                        {
                            defineId: "voucherInfoDifine",
                            span: 8,
                            fields: ["voucherSerialNumber", "fiscalYear", "mainBusiness", "explanation"]
                        },
                        {
                            defineId: "voucherInfoDifine",
                            span: 8,
                            fields: ["voucherNo", "accountingPeriod", 'childBusiness']
                        },
                        {defineId: "voucherInfoDifine", span: 8, fields: ["contractNo", "totalAmount", "sendingDate"]},
                    ]
                }],
                entryColumns: [
                    'entryNo', 'lendingDirection', 'subjectCode', 'subjectName', 'debitAmt', 'creditAmt', 'explanation'
                ],
                parm: {
                    pageNumber: 1,
                    pageSize: 10,
                },
                dataForExport: [],
                data: [],
                columns: [
                    {title: "凭证号", key: "voucherNo",type:"text", minWidth: 180, align: "center"},
                    {title: "合同号", key: "contractNo", minWidth: 180, align: "center"},
                    {title: "记账日期", key: "postingDate", minWidth: 150, align: "center"},
                    {title: "分录序号", key: "entryNo", minWidth: 150, align: "center"},
                    {title: "科目代码", key: "subjectCode", minWidth: 150, align: "center"},
                    {title: "科目名称", key: "subjectName", minWidth: 200, align: "center"},
                    {title: "借贷方向", key: "lendingDirection", minWidth: 200, align: "center"},
                    {title: "借方金额", key: "debitAmt", minWidth: 200, align: "center"},
                    {title: "贷方金额", key: "creditAmt", minWidth: 200, align: "center"},
                    {title: "摘要", key: "explanation", minWidth: 200, align: "center"},
                    {title: "状态", key: "status", minWidth: 200, align: "center"},
                ],
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "voucherInfoDifine",
                        fields: voucherInfoDifine
                    },
                    {
                        id: "voucherEntryDifine",
                        fields: voucherEntryDifine
                    }
                ];
            }
        },
        methods: {
            queryData(queryData) {
                console.log(queryData);
                this.parm.pageNumber = queryData.pageNumber;
                this.parm.pageSize = queryData.pageSize;
                getAccountVoucherInfoList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.infoTable.updateTableData(records, total);
                    }
                });
            },

            refresh() {
                this.$refs['infoTable'].reloadData();
            },
            view(row) {
                this.isShowModel = true;
                this.$refs.infoForm.resetFrom();
                this.$refs.infoForm.setAllFieldReadOnly(true);
                //获取凭证明细信息
                getVoucherInfo(row.id).then(res => {
                    if (res.code === "0000") {
                        this.$refs.infoForm.updateFormData(res.data.accountVoucherInfo);
                        this.$refs.entryTable.updateTableData(res.data.entryList);
                    }
                });
            },
            //红冲
            reversal(v) {
                this.$Modal.confirm({
                    title: "红冲凭证",
                    content: "您确认要红冲凭证?",
                    onOk: () => {
                        this.operationLoading = true;
                        reversal(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("红冲凭证成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            //重新发送
            resend(v) {
                this.$Modal.confirm({
                    title: "重新发送",
                    content: "您确认要设置重新发送?",
                    onOk: () => {
                        this.operationLoading = true;
                        resend(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("设置重新发送成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            exportData() {
                this.dataForExport = [];
                this.parm.condition = this.$refs.searchForm.formData;
                exportVoucherList(this.parm).then(res => {
                    if (res.code === "0000") {
                        this.dataForExport = res.data;
                        this.dataForExport.forEach(line =>{
                            line.voucherNo = "\t" + line.voucherNo;
                        });
                        this.$refs.voucherInfo.exportCsv({
                            filename: '凭证信息',
                            columns: this.columns,
                            data: this.dataForExport
                        });
                    } else {
                        this.$Message.error("导出失败");
                    }
                });
            },
        },
        mounted() {
            this.refresh();
        }
    };
</script>
